package com.woniuxy.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniuxy.domain.Dealtime;
import com.woniuxy.dto.*;
import com.woniuxy.parameter.WorkingDayParam;
import com.woniuxy.parameter.WorkingMounthParam;
import com.woniuxy.service.DealtimeService;
import com.woniuxy.util.BeanCopyUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * @ClassName WorkingHoursController
 * @Description 月/日工作时间统计控制器(统计报表)
 * @Author Joker
 * @Version 1.0.0
 **/
@RestController
@RequestMapping("/workingHours")
@Api(tags = "工时统计")
public class WorkingHoursController {
    @Resource
    private DealtimeService dealtimeService;
    /**
     * @Author Joker
     * @Description 根据前端传回来的年月统计该年月的月工作时间
     * @Date  
     * @Param [yearTime, monthTime] 年时间,月时间
     * @Return com.woniuxy.dto.JSONResult
     **/
    @ApiOperation("统计月工时")
    @GetMapping("/month")
    public DtoJSONResult getMonthWorkingHours(WorkingMounthParam workingMounthParam){
        QueryWrapper<Dealtime> wrapper = new QueryWrapper<>();
        Page page = new Page<Dealtime>(workingMounthParam.getPageNum(),workingMounthParam.getPageSize());

        //前端传回来的是String数据是两个输入框的数据,年和月输入传回来
        String startTime = workingMounthParam.getYearTime() + "-" + workingMounthParam.getMonthTime() + "-" + "1";
        String endTime = workingMounthParam.getYearTime() + "-" + workingMounthParam.getMonthTime() + "-" + "31";
        wrapper.eq("record_state","完成");
        wrapper.lt("endtime",endTime);
        wrapper.gt("starttime",startTime);
        Page page1 = dealtimeService.page(page, wrapper);
        DtoPageInfomation dtoPageInfo = new DtoPageInfomation();
        dtoPageInfo.setPages(page1.getPages());
        dtoPageInfo.setPageCount(page1.getTotal());
        dtoPageInfo.setCurrent(page1.getCurrent());
        dtoPageInfo.setSize(page1.getSize());
        System.out.println(dtoPageInfo);
        List records = page1.getRecords();
        return new DtoJSONResult(DtoJSONResultCode.SUCCESS.getCode(), DtoJSONResultCode.SUCCESS.getMessage(),dtoPageInfo,records);
    }
    /**
     * @Author Joker
     * @Description
     * @Date  根据前端传回来的年月统计xx-xx-xx到xx-xx-xx时间内的工作时间
     * @Param [time1, time2]前端传回来的起始时间,结束时间
     * @Return com.woniuxy.dto.JSONResult
     **/
    @ApiOperation("统计日工时")
    @GetMapping("/day")
    public DtoJSONResult getDayWorkingHours(WorkingDayParam woringDayParam){
        QueryWrapper<Dealtime> wrapper = new QueryWrapper<>();
        Page page = new Page<Dealtime>(woringDayParam.getPageNum(),woringDayParam.getPageSize());
        String time1=woringDayParam.getTime1();
        String time2=woringDayParam.getTime2();
        //字符串转换,因为查询日期,如最大查询时间为2021-05-04 则日期需要+1才能查询到2021-05-04的提交信息
        String str = "0"+(Integer.parseInt(time2.split("-")[2])+1);
        time2 = time2.replace(time2.substring(time2.lastIndexOf("-")+1),str);
        wrapper.lt("endtime",time2);
        wrapper.gt("starttime",time1);
        Page page1 = dealtimeService.page(page, wrapper);
        DtoPageInfomation dtoPageInfo = new DtoPageInfomation();
        dtoPageInfo.setPages(page1.getPages());
        dtoPageInfo.setPageCount(page1.getTotal());
        dtoPageInfo.setCurrent(page1.getCurrent());
        dtoPageInfo.setSize(page1.getSize());
        System.out.println(dtoPageInfo);
        List records = page1.getRecords();
        return new DtoJSONResult(DtoJSONResultCode.SUCCESS.getCode(), DtoJSONResultCode.SUCCESS.getMessage(),dtoPageInfo,records);
    }
}
